package com.cockybook.utils;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {

    /**
     * eg:/chaxun/zuozhe/{ID}.html
     * @return  ID
     */
    public static String getAuthID(String link){
        int start = link.lastIndexOf("/");
        int end  = link.indexOf(".");
        return link.substring(start+1,end);

    }

    /**
     * /category/{tangdaishiren}
     * @param str
     * @return
     */
    public static String getDynasty(String str){
        if(str.length()<10){
            return "none";
        }

        return str.substring(10);

    }


    public static int getAuthIntID(String link) throws Exception {
        String value = getAuthID(link);
        if(StringUtils.isAlphanumeric(value)){
            return Integer.parseInt(value);
        }else {
            throw new Exception("Error: "+ value);
        }


    }

    /**
     * eg: xxx(123)
     * @param text
     * @return   xxx
     */
    public static String getAuthIntName(String text) throws Exception {
        try {
            int end  = text.indexOf("(");
            if(end==-1){
                return text;
            }else {
                return text.substring(0,end);
            }

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("text:"+text);
            throw e;
        }


    }


    public static String getPoemName(String title) {

        if(title.length()<2){
            return title;
        }
        return title.substring(1,title.length()-1);

    }
}
